/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Random;

/**
 * <p>Project: app2022 - ImgUtils
 * <p>Powered by webrx On 2022-01-24 14:06:18
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ImgUtils {
    private int sw = 0;
    private int sh = 0;
    private BufferedImage src;
    private String img;
    private String ext;

    public ImgUtils(File img) {
        this.setImg(img);
    }

    public ImgUtils(String img) {
        this.setImg(img);
    }

    public ImgUtils() {

    }

    public void setImg(File img) {
        this.img = img.getAbsolutePath();
        try {
            this.src = ImageIO.read(img);
            this.sw = this.src.getWidth();
            this.sh = this.src.getHeight();
            this.ext = this.img.substring(this.img.lastIndexOf(".") + 1).toLowerCase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setImg(String img) {
        this.img = img;
        try {
            this.src = ImageIO.read(new FileInputStream(this.img));
            this.sw = this.src.getWidth();
            this.sh = this.src.getHeight();
            this.ext = this.img.substring(this.img.lastIndexOf(".") + 1).toLowerCase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void maskText(String text, int fontsize, String color, int pos, boolean append) {
        var rand = new Random();
        //设置字体，字号，样式
        var g = this.src.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        var f = new Font("微软雅黑", Font.BOLD, fontsize);
        g.setFont(f);

        //设置位置
        var fm = g.getFontMetrics(f);
        var tw = fm.stringWidth(text);
        int sp = 10;
        int x = 0;
        int y = 0;
        switch (pos) {
            case 1:
                x = sp + 0;
                y = fm.getAscent() - fm.getDescent() + sp;
                break;
            case 2:
                x = 0;
                y = 0;
                break;
            case 3:
                x = 0;
                y = 0;
                break;
            case 4:
                x = 0;
                y = 0;
                break;
            case 5:
                x = 0;
                y = 0;
                break;
            case 6:
                x = 0;
                y = 0;
                break;
            case 7:
                x = 0;
                y = 0;
                break;
            case 8:
                x = 0;
                y = 0;
                break;
            case 9:
                x = 0;
                y = 0;
                break;
            default:

                break;
        }

        //设置颜色
        g.setColor(getColor("rand", 1f));

        g.drawString(text, x, y);
        g.dispose();

        save(append);

    }

    public void save(boolean append) {
        try {
            if (append) {
                ImageIO.write(this.src, this.ext, new File(this.img));
            } else {
                var fn = this.img.substring(0, this.img.lastIndexOf(".")).concat("_txt." + this.ext);
                ImageIO.write(this.src, this.ext, new File(fn));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Color getColor(String color, float alpha) {
        //#ff000033  red new Color(255,0,0)  white new Color(255,255,255) black new Color(0,0,0);
        Color c = new Color(255, 255, 255, 255);
        if ("rand".equals(color)) {
            Random rand = new Random();
            c = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        }

        return c;
    }


    public void maskLogo() {

    }
}
